var fs = require('fs');
var code = require('./code');

function cleanCache(moduleName) {
    var id = require.resolve(moduleName);
    moduleObj = require.cache[id];

    require.cache[id] = null;
    try {
        code = require('./code');
        if (moduleObj.parent)
            moduleObj.parent.children.splice(moduleObj.parent.children.indexOf(moduleObj), 1);
    } catch (e) {
        require.cache[id] = moduleObj;
    }
}

setInterval(() => {
    console.log("-----------------------");
}, 5000);

fs.watchFile(require.resolve('./code'), (curStats, preStats) => {
    if(curStats.mtime == preStats.mtime) return;

    console.log(curStats, preStats);
    cleanCache('./code');
    console.log(code);
});
